<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<form>

    服务器地址：
    <input type="plaintext" id="addr" value="192.168.244.158" />
    端口号：
    <input type="plaintext" id="port" value="13999" />

    <br />

    输入消息：
    <input type="plaintext" id="input" value="" />
    <br />

    输出消息：
    <input type="plaintext" id="echo" />
    </form>
    <script type="text/javascript" src="message.js"></script>
    <script type="text/javascript">
//    var type_list={MESSAGE:{},LOGIN:{}};
//    type_list["LOGIN"]={LOGIN_INFO:{},RETURN:{}};

    var isconnected = false;
    var input = document.getElementById("input");
    var echo = document.getElementById("echo");
    var wsock;

    var plainmsg = {"head":"","record":"","expand":""};
    plainmsg["head"]={"tag":"MESG","version":65537};
    var jsonstr = JSON.stringify(plainmsg);

    function mywebsockinit() {
        if (isconnected) {
            alert("已连接服务器！")
            return;
        }
        var netaddr=document.getElementById("addr");
        var netport=document.getElementById("port");

 //       wsock = new WebSocket('ws://192.168.244.158:13999', 'cube-wsport');
        wsock = new WebSocket('ws://'+netaddr.value+':'+netport.value+'/');

        wsock.onopen = function (e) {
            if (!isconnected) {
                isconnected = true;
                alert("连接成功！")
            }
            return;
        };
        wsock.onclose = function (e) {
        };
        wsock.onerror = function (e) {
        };
        wsock.onmessage = function (e) {
            var msg;
            msg = JSON.parse(e.data);
  //          if (msg.replace(/(^s*)|(s*$)/g, "").length != 0) {
   //             alert(msg);
   //         }
 //           for (var key in msg.RECORD) {
                if (msg.RECORD[0].uuid != null) {
                    document.getElementById("echo").value = msg.RECORD[0].uuid;
                    document.getElementById("echo").value += '\n';

                }
  //          }
        }
    }

    function myFunction() {
        if(!isconnected)
        {
            alert("连接未建立！")
            return;
        }

        var message = {message:input.value};
		var key_req={"issmkwrapped":"1","key_usage":"TCM_SM2KEY_SIGNING","key_flags":"TCM_PCRIGNOREDONREAD"};
        var msg = new Cube_msg("TCM_KEY_MANAGE","PRIVATE_KEY");
        msg.addrecord(key_req);
        wsock.send(msg.output())
    };
</script>
<button type="button" onclick="mywebsockinit()" id="connect">连接服务器</button>
<button type="button" onclick="myFunction()" id="sendmsg">申请密钥</button>
</body>
</html>